package com.lowca.news.web.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.codehaus.jackson.map.ObjectMapper;
import org.compass.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lowca.news.dao.support.Pager;
import com.lowca.news.entity.Article;
import com.lowca.news.service.ArticleService;
import com.lowca.news.web.formbean.AddArticleForm;
import com.lowca.news.web.formbean.AdminArticleConditionForm;
import com.lowca.news.web.jsonbean.ArticleWarpperBean;

@Controller
public class AdminArticleController {

	@Resource
	private ArticleService articleService;

	@RequestMapping(value = "/admin/article/list.do", method = RequestMethod.GET)
	public String listArticle(HttpServletRequest request)
			throws ServletException, IOException {
		return "/admin/article/list";
	}

	@RequestMapping(value = "/admin/article/list.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listArticleSubmit(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "jCondition", required = false) String jCondition,
			@RequestParam(value = "start", required = true) int start,
			@RequestParam(value = "limit", required = true) int limit)
			throws ServletException, IOException {
		AdminArticleConditionForm condition = null;
		if (StringUtils.hasText(jCondition)) {
			ObjectMapper mapper = new ObjectMapper();
			mapper.registerSubtypes(AdminArticleConditionForm.class);
			condition = mapper.readValue(jCondition,
					AdminArticleConditionForm.class);
		}
		int pageNo = start / limit + 1;
		Pager<Article> articlePage = articleService.searchArticle(condition,
				pageNo, limit);
		List<ArticleWarpperBean> list = warpArticle(articlePage);
		Map<String, Object> jMap = new HashMap<String, Object>();
		jMap.put("count", articlePage.getTotalCount());
		jMap.put("dataset", list);
		return jMap;
	}

	private List<ArticleWarpperBean> warpArticle(Pager<Article> page) {
		List<ArticleWarpperBean> waperList = new ArrayList<ArticleWarpperBean>();
		for (Article article : page.getList()) {
			waperList.add(new ArticleWarpperBean(article));
		}
		return waperList;
	}

	@RequestMapping(value = "/admin/article/add.do", method = RequestMethod.GET)
	public String addArticle(HttpServletRequest request,
			Map<String, Object> model) throws ServletException, IOException {
		AddArticleForm form = new AddArticleForm();
		model.put("form", form);
		return "/admin/article/add";
	}

	@RequestMapping(value = "/admin/article/add.do", method = RequestMethod.POST)
	public ModelAndView addArticleSubmit(HttpServletRequest request,
			@ModelAttribute("form") @Valid AddArticleForm form,
			BindingResult result) throws ServletException, IOException {
		if (result.hasErrors()) {
			return new ModelAndView("/admin/article/add");
		}
		return new ModelAndView("/admin/article/list-success");
	}

}
